import Vue from 'vue'
import VueRouter from 'vue-router'
// 引入登录组件
import Login from '../components/Login.vue'
// 引入主页组件
import Home from '../components/Home.vue'
// 引入欢迎组件
import Welcome from '../components/Welcome.vue'
// 引入用户列表组件
import Users from '../components/user/Users.vue'
// 引入权限列表组件
import Rights from '../components/power/Rights.vue'
// 引入角色列表组件
import Roles from '../components/power/Roles.vue'
// 引入商品分类组件
import Category from '../components/goods/Category.vue'
// 引入商品参数组件
import Params from '../components/goods/Params.vue'
// 引入商品列表组件
import List from '../components/goods/List.vue'
// 引入添加商品组件
import Add from '../components/goods/Add.vue'
// 引入订单组件
import Order from '../components/order/Order.vue'
// 引入报表组件
import Report from '../components/report/Report.vue'

Vue.use(VueRouter)

const router = new VueRouter({
  routes:[
    // 重定向路由规则
    {path:'/',redirect:'/login'},
    {path:'/login',component:Login},
    {
      path:'/home',
      // meta:{isAuth:true},
      component:Home,
      // 只要访问到 home，就重定向到 welcome
      redirect:'/welcome',
      children:[
        {path:'/welcome',component:Welcome},
        {path:'/users',component:Users},
        {path:'/rights',component:Rights},
        {path:'/roles',component:Roles},
        {path:'/categories',component:Category},
        {path:'/params',component:Params},
        {path:'/goods',component:List, },
        {path:'/goods/add',component:Add},
        {path:'/orders',component:Order},
        {path:'/reports',component:Report}
      ]
    },
  ]
})

// 全局前置路由守卫
router.beforeEach((to,from,next)=>{
  // if(to.meta.isAuth){
  //   if(sessionStorage.getItem('token'))
  // }
    //访问登录页面，放行 
    if(to.path==='/login') return next()
    //如果不是登录页面，则需要判断是否有token，没有则跳转登陆页面 
    const tokenStr = sessionStorage.getItem('token')
    if(!tokenStr) return next('/login')
    next()
})

export default router
